<?php
//No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.view');

class OwnserviceViewUser extends JView
{
	function display($tpl=null) {

		global $mainframe;

		// Initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
		$document	= & JFactory::getDocument();
		$params 	= & $mainframe->getParams('com_ownservice');

		// Make sure you are logged in and have the necessary access rights
		if($user->gid < 25) {
			if (!Ownservice::checkAccess( 'user', 'manage' )) {
			  JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
			  $mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			}
		}

		if( $this->getLayout() == 'form' ) {
			$this->_displayForm($tpl);
			return;
		} 

		//add style to document
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/nav.css');
		$document->addScriptDeclaration(
									"
										window.addEvent('domready', function() {
											SqueezeBox.initialize();
											$$('a.img-preview').each(function(el) {
												el.addEvent('click', function(e) {
													new Event(e).stop();
													SqueezeBox.fromElement(el);
												});
											});
										});
									");

		//Get data form the model
		$model		= $this->getModel();
		$row		= $this->get('Data');
		$files		= $model->getFiles($row->user_id);

		// Create a user access object for the current user
		$access = new stdClass();
		$access->canEditOwn	= Ownservice::checkAccess('account','edit','account','own');
		$access->canEdit	= ($user->gid < 25)? Ownservice::checkAccess('account','edit','account','all'):1;

		//set page title
		$params->set('page_title',	JText::_('OWNSERVICE_USER_DETAIL'));
		$document->setTitle( $params->get('page_title') );

		JHTML::_('behavior.modal', 'a.modal');

		$this->assignRef('user', $user);
		$this->assignRef('ownuser', $ownuser);
		$this->assignRef('row', $row);
		$this->assignRef('files', $files);
		$this->assignRef('access', $access);

		parent::display($tpl);
	}

	function _displayForm() {
		global $mainframe, $option;

		// Initialise variables
		$user		= & JFactory::getUser();
		$gid		= $user->get('gid');
		$acl		= & JFactory::getACL();
		$ownuser	= & Ownservice::getUser();
		$document	= & JFactory::getDocument();
		$params 	= & $mainframe->getParams('com_ownservice');

		//get var
		$level = $ownuser->get('level_id');

		//add style to document
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/nav.css');
		$document->addScript(JURI::base().'components/com_ownservice/assets/js/ownservice.js');
		$document->addScriptDeclaration(
									"
										function addUploadFile() {
											var li 		= document.createElement('li');
											var file	= document.createElement('input');
											var buttondel = document.createElement('input');
											var buttonadd = document.createElement('input');

											buttondel.type	= 'button';
											buttondel.name	= 'removebutton';
											buttondel.id	= 'removebutton';
											buttondel.style.border = '1px';
											buttondel.style.borderStyle='solid';
											buttondel.style.borderColor='silver';
											$(buttondel).addEvent('click', function() { deleteFile(this) });
											buttondel.value = '".JText::_( 'OWNSERVICE_REMOVE_FILE' )."';

											buttonadd.type = 'button';
											buttonadd.name	= 'addbutton';
											buttonadd.id	= 'addbutton';
											buttonadd.style.border = '1px';
											buttonadd.style.borderStyle='solid';
											buttonadd.style.borderColor='silver';
											$(buttonadd).addEvent('click', function() { addUploadFile() });
											buttonadd.value = '".JText::_( 'OWNSERVICE_ADD_FILE' )."';

											file.type	= 'file';
											file.name	= 'Filedata[]';

											var filelist = document.getElementById('addfile');

											filelist.appendChild(li);
											li.appendChild(file);
											//li.appendChild(buttonadd);
											li.appendChild(buttondel);
										}

										function deleteFile(el) {
											var file	= $(el);
											var row		= file.getParent();
											var fx		= row.effects({duration: 300, transition: Fx.Transitions.linear});
											
											fx.start({
												'height': 0,
												'opacity': 0			
											}).chain(function(){
												row.remove();
											});
										}

										window.addEvent('domready', function() {
											SqueezeBox.initialize();
											$$('a.img-preview').each(function(el) {
												el.addEvent('click', function(e) {
													new Event(e).stop();
													SqueezeBox.fromElement(el);
												});
											});
										});
									"
								);

		//Get data form the model
		$model		= $this->getModel();
		$row		= & $this->get('Data');
		$currencies	= & $this->get('Currencies');
		$countries	= & $this->get('Countries');
		$groups		= & $this->get('Groups');
		$branches	= & $this->get('Branches');
		$files		= $model->getFiles($row->user_id);

		// Create a user access object for the current user
		$access = new stdClass();
		if($row->id) {
			$access->canEdit	= ($gid<25)?Ownservice::checkAccess('user', 'edit', 'user', 'all'):1;
			if (!$access->canEdit) {
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			}
		} else {
			$access->canAdd		= ($gid<25)?Ownservice::checkAccess('user', 'add', 'user', 'all'):1;
			if (!$access->canAdd) {
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			}
		}

		$access->canChangeBranch = ($gid<25)?Ownservice::checkAccess('user','changebranch','user','all'):1;

		//set page title
		if($row->id) { 
			$params->set('page_title',	JText::_('OWNSERVICE_EDIT_USER'));
		} else { 
			$params->set('page_title',	JText::_('OWNSERVICE_NEW_USER'));
		} 
		$document->setTitle( $params->get('page_title') );

		$lists = array();

		//build ownservices groups select list
		$lists['group'] = OwnserviceHelper::buildgroupsselect($groups, 'group_id',$row->group_id, 1, 'class="inputbox" style="width:250px;"');

		//build branch select list
		$lists['branch'] = OwnserviceHelper::buildbranchesselect($branches,'branch_id', $row->branch_id, 1, 'class="inputbox" style="width:250px;"');

		//build currency exchange list
		$lists['currency'] = OwnserviceHelper::buildcurrencyselect($currencies, 'currency', $row->currency, true, 'class="inputbox" style="width:250px;"');

		//build country select list
		$lists['country'] = OwnserviceHelper::buildcountryselect($countries, 'country',$row->country,1, 'class="inputbox" style="width:250px;"');

		//build id type select list
		$lists['id_type'] = OwnserviceHelper::buildidtypeselect('id_type', $row->id_type, 1, 'class="inputbox" style="width:250px;"');
		
		//build issuer select list
		$lists['issuer'] = OwnserviceHelper::buildissuerselect('issuer', $row->issuer, 1, 'class="inputbox" style="width:250px;"');

		JHTML::_('behavior.modal', 'a.modal');

		$this->assignRef('user', $user);
		$this->assignRef('ownuser', $ownuser);
		$this->assignRef('row', $row);
		$this->assignRef('lists', $lists);
		$this->assignRef('access', $access);
		$this->assignRef('profile', $profile);
		$this->assignRef('files', $files);

		parent::display($tpl);
	}
}